/**
 * @author janet
 * @date 2015-5-4
 */
package net.oscorner.controller.menu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.oscorner.commons.BaseController;
import net.oscorner.commons.ConstantUtil;
import net.oscorner.commons.SpringsUtils;
import net.oscorner.entiry.Menus;
import net.oscorner.services.menu.MenuService;
import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


/**
 * @author janet
 * @date 2015-5-4 下午3:53:08
 * 
 */
@Controller
@RequestMapping("/menu/*")
public class MenuController extends BaseController{
	@Autowired
	private HttpServletRequest request;
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	@RequestMapping(method=RequestMethod.GET,value="add")
	public String addMenu(HttpServletResponse response) throws IOException{
		return "menu/add";
	}
	
	@RequestMapping(method= RequestMethod.POST,value="add")
	public String addMenu(HttpServletRequest request,HttpServletResponse response,Menus entity){
		return "redirect:"+ConstantUtil.BASEPATH + "index";
	}
	@RequestMapping(value="list")
	public ModelAndView list(HttpServletResponse response,Menus entity){
		List<Menus> list = getService().queryMenus(entity);
		ModelAndView mv = new ModelAndView("menu/list");
		mv.addObject("query", list);
		mv.addObject("list", list);
		JSONArray ja = JSONArray.fromObject(list);
		return mv;
	}
	
	@RequestMapping("plist")
	public String list(HttpServletResponse response,Menus e,HttpServletRequest request) throws IOException{
		List<Menus> list = getService().queryMenus(e);
		JSONArray ja = JSONArray.fromObject(list);
		String json = "{\"draw\":1,\"\":100,\"\":100,\"data\":"+ja.toString()+"}";
		response.getWriter().print(json);
		return "menu/list";
	}
	private MenuService getService(){
		MenuService ms = (MenuService)SpringsUtils.getBean("menuService");
		return ms;
	}
}
